package com.huike.pmps.controller;

import com.huike.pmps.model.dto.common.PageInfo;
import com.huike.pmps.model.entity.Member;
import com.huike.pmps.model.entity.PO;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.POCourseService;
import com.huike.pmps.service.POService;
import com.huike.pmps.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@RestController
@RequestMapping("/api/pos")
@Api(tags = "PO管理")
public class POController {

    @Autowired
    POService poService;
    @Autowired
    POCourseService poCourseService;

    @ApiOperation(value = "按搜索条件分页查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "po", value = "po单号", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "customerName", value = "客户名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "contractNo", value = "合同编号", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "regionName", value = "战区名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "provinceName", value = "省份名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "teamName", value = "客户成功名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "poState", value = "po状态(1未启动、2进行中、3已交付、4已验收、5已完结、6已终止、7已过期)", paramType = "query", dataType = "int")
    })
    @GetMapping
    public Result selectByCondition(HttpServletRequest request, PageInfo pageInfo, String po, String customerName, String contractNo, String regionName, String provinceName, String teamName,Integer poState) {
        //从请求头中获取token
        Member member = (Member) request.getSession().getAttribute(TokenUtil.getToken(request));
        return poService.selectByCondition(member, pageInfo, po, customerName, contractNo, regionName, provinceName,teamName, poState);
    }

    @ApiOperation(value = "根据ID查询PO详情", httpMethod = "GET")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        return poService.selectById(id);
    }

    @ApiOperation(value = "根据ID更新PO数据", httpMethod = "PUT")
    @PutMapping
    public Result updateById(PO po) {
        return poService.updateById(po);
    }

    @ApiOperation(value = "删除PO", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        return poService.deleteById(id);
    }

    @ApiOperation(value = "根据id查询po下的课程", httpMethod = "GET")
    @GetMapping("/{id}/courses")
    public Result selectByPoId(@PathVariable("id") Integer id) {
        return poCourseService.selectByPoId(id);
    }
}
